﻿using System;
using System.Collections.Generic;
using System.IO;
using ifunction.BaiduBCS.SDK;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ifunction.BaiduBCS.UnitTest
{
    /// <summary>
    /// Class BaiduBCSUnitTest.
    /// </summary>
    partial class BaiduBCSUnitTest
    {
        /// <summary>
        /// The bucket
        /// </summary>
        const string bucket = "AvatarCollection";
        /// <summary>
        /// The object name
        /// </summary>
        const string objectName = "test2.txt";

        /// <summary>
        /// Creates the object.
        /// </summary>
        [TestMethod]
        public void Test()
        {
            File.WriteAllText(@"D:\" + objectName, "Test string \n\r Hello world.");

            byte[] content = File.ReadAllBytes(@"D:\" + objectName);

            var md5 = client.CreateStorageObject(new StorageObjectUploadRequest
                  {
                      Bytes = content,
                      BucketName = bucket,
                      ObjectName = objectName,
                      DownloadFileName = "Morning.noon",
                      ContentType = "text/plain",
                      IsSuperFile = false,
                      Permission = StorageObjectPermission.PublicReadAndWrite
                  });

            var data = client.GetStorageObject(new StorageObjectDownloadRequest
            {
                BucketName = bucket,
                ObjectName = objectName
            });

            client.DeleteStorageObject(new StorageObjectIdentity
            {
                BucketName = bucket,
                ObjectName = objectName
            });
        }

        /// <summary>
        /// Gets the storage object meta information.
        /// </summary>
        [TestMethod]
        public void GetStorageObjectMetaInfo()
        {
            File.WriteAllText(@"D:\" + objectName, "Test string \n\r Hello world.");

            byte[] content = File.ReadAllBytes(@"D:\" + objectName);

            var md5 = client.CreateStorageObject(new StorageObjectUploadRequest
            {
                Bytes = content,
                BucketName = bucket,
                ObjectName = objectName,
                DownloadFileName = "Morning.noon",
                ContentType = "text/plain",
                IsSuperFile = false,
                Permission = StorageObjectPermission.PublicReadAndWrite
            });

            var meta = client.GetStorageObjectMetaInfo(new StorageObjectDownloadRequest
            {
                BucketName = bucket,
                ObjectName = objectName
            });
        }

        /// <summary>
        /// Gets the storage object list.
        /// </summary>
        [TestMethod]
        public void GetStorageObjectList()
        {
            var result = client.GetStorageObjectList(new StorageObjectCriteria
            {
                Count = 100,
                Start = 0,
                BucketName = bucket
            });
        }

        /// <summary>
        /// Copies the and delete object.
        /// </summary>
        [TestMethod]
        public void CopyAndDeleteObject()
        {
            const string newObjectName = "copiedObject.txt";

            client.CopyStorageObject(new StorageObjectCopyRequest
                       {
                           ObjectName = "Test.txt",
                           NewObjectName = newObjectName,
                           NewBucketName = bucket,
                           BucketName = bucket
                       });
        }

        /// <summary>
        /// Creates the super object by bytes.
        /// </summary>
        [TestMethod]
        public void CreateSuperObjectByBytes()
        {
            // This is just a test. We need to get bytes.
            File.WriteAllText(@"D:\" + objectName, "Test string \n\r Hello world. Large File!!");

            byte[] content = File.ReadAllBytes(@"D:\" + objectName);


            // If you set [deletePartsWhenFinish] as false, all small pieces of large file would be remained.
            // Default value for [deletePartsWhenFinish ] is [True].
            var md5 = client.CreateSuperStorageObject(new StorageObjectUploadRequest
                     {
                         BucketName = bucket,
                         ObjectName = objectName,
                         Bytes = content
                     }, 10, true);
        }

        /// <summary>
        /// Creates the super object by file path.
        /// </summary>
        [TestMethod]
        public void CreateSuperObjectByFilePath()
        {
            // This is just a test. We need to get bytes.
            File.WriteAllText(@"D:\" + objectName, "Test string \n\r Hello world. Large File!!");

            // If you set [deletePartsWhenFinish] as false, all small pieces of large file would be remained.
            // Default value for [deletePartsWhenFinish ] is [True].
            var md5 = client.CreateSuperStorageObject(new StorageObjectIdentity
            {
                BucketName = bucket,
                ObjectName = objectName
            }, @"D:\" + objectName, 10, true);
        }
    }
}
